package cn.ezios.huo.system.biz.role;

import cn.ezios.huo.system.dao.domain.SysRoleMenu;
import cn.ezios.huo.system.dao.domain.SysUserRole;
import cn.ezios.huo.system.dao.service.SysRoleMenuService;
import cn.ezios.huo.system.dao.service.SysUserRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class RoleService {

    private final SysUserRoleService userRoleService;
    private final SysRoleMenuService roleMenuService;

    /**
     * 给用户添加角色
     *
     * @return
     */
    public boolean addRolesToUser(String userId, String[] roleIds) {
        List<SysUserRole> userRoles = Arrays.stream(roleIds).map(roleId -> new SysUserRole()
                .setUserId(userId)
                .setRoleId(roleId)
        ).toList();
        return userRoleService.saveBatch(userRoles);
    }

    /**
     * 给角色添加菜单
     *
     * @return
     */
    public boolean addMenusToRole(String roleId, String[] menuIds) {
        List<SysRoleMenu> userRoles = Arrays.stream(menuIds).map(menuId -> new SysRoleMenu()
                .setRoleId(roleId)
                .setMenuId(menuId)
        ).toList();
        return roleMenuService.saveBatch(userRoles);
    }

}
